package app.beit.finportal.controller.backdoor;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import app.beit.finportal.bean.AccountBean;
import app.beit.finportal.bean.RelativeWebProgramsBean;
import app.beit.finportal.controller.BaseController;
import app.beit.finportal.controller.FintrackResponse;
import app.beit.finportal.service.RelativeWebProgramsService;
import app.beit.finportal.utils.constr.MsgConstr;
import app.beit.finportal.utils.constr.RelativeWebProgramsConstr;
import app.beit.finportal.utils.constr.SessionConstr;

@Controller
@RequestMapping("/backend/programs")
public class BackdoorRelativeProgramsController extends BaseController {

	@Autowired
	private RelativeWebProgramsService relativeWebProgramsService;
	
	@RequestMapping
	public String main(final HttpServletRequest request){
		super.removeSessionNotUser(request);
		request.setAttribute(SessionConstr.ACTIVE_PAGE, "programs");
		return "backend.relativePrograms";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="getAll", method=RequestMethod.POST)
	@ResponseBody
	public FintrackResponse<List<RelativeWebProgramsBean>> getAll(){
		FintrackResponse<List<RelativeWebProgramsBean>> response = null;
		try{
			final List<RelativeWebProgramsBean> beans = this.relativeWebProgramsService.getAllByTypesBackdoor(RelativeWebProgramsConstr.TYPE_PROGRAM);
			if(beans != null && beans.size() > 0){
				response = super.responseSuccess();
				response.setDatas(beans);
			}else{
				response = super.responseSuccessNullData();
			}
		}catch(Exception e){
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
			e.printStackTrace();
		}
		return response;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value="add", method = RequestMethod.POST, headers = {"Content-type=application/json"})
	@ResponseBody
	public FintrackResponse<RelativeWebProgramsBean> add(final HttpServletRequest request, @RequestBody final RelativeWebProgramsBean bean){
		FintrackResponse<RelativeWebProgramsBean> response = null;
		try{			
			//set user for create and update
			final AccountBean user = (AccountBean) request.getSession().getAttribute(SessionConstr.USER);
			bean.setCreatedBy(user.getUsername());
			bean.setCreatedDate(new Date(System.currentTimeMillis()));
			bean.setUpdatedBy(user.getUsername());
			bean.setUpdatedDate(new Date(System.currentTimeMillis()));
			
			bean.setTypes(RelativeWebProgramsConstr.TYPE_PROGRAM);
			
			this.relativeWebProgramsService.add(bean);
			response = super.responseSuccess();
			response.setDatas(bean);
		}catch(Exception e){
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
			e.printStackTrace();
		}
		return response;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value="update", method=RequestMethod.POST)
	@ResponseBody
	public FintrackResponse<RelativeWebProgramsBean> update(final HttpServletRequest request, @RequestBody final RelativeWebProgramsBean bean){
		FintrackResponse<RelativeWebProgramsBean> response = null;
		try{			
			//set user for create and update
			final AccountBean user = (AccountBean) request.getSession().getAttribute(SessionConstr.USER);
			bean.setUpdatedBy(user.getUsername());
			bean.setUpdatedDate(new Date(System.currentTimeMillis()));

			bean.setTypes(RelativeWebProgramsConstr.TYPE_PROGRAM);
			this.relativeWebProgramsService.update(bean);
			response = super.responseSuccess();
			response.setDatas(bean);
		}catch(Exception e){
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
			e.printStackTrace();
		}
		return response;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value="delete", method=RequestMethod.POST)
	@ResponseBody
	public FintrackResponse<RelativeWebProgramsBean> delete(final HttpServletRequest request, final @RequestParam("id") String id){
		FintrackResponse<RelativeWebProgramsBean> response = super.responseError(MsgConstr.CANNOT_DELETE_DATA_CODE, MsgConstr.CANNOT_DELETE_DATA_MSG);
		try{
			final RelativeWebProgramsBean bean = new RelativeWebProgramsBean();
			bean.setId(Long.parseLong(id));
			this.relativeWebProgramsService.delete(bean);
			response = super.responseSuccess();
		}catch(Exception e){
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
			e.printStackTrace();
		}
		return response;
	}
	
}
